package it.hopeless.jee6.registry;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.component.html.HtmlInputText;
import javax.faces.context.FacesContext;
import javax.faces.event.AjaxBehaviorEvent;
import java.io.Serializable;

/**
 * User: frico
 * Date: Feb 6, 2010
 * Time: 7:38:36 PM
 */
@ManagedBean(name="registryMonitor")
public class RegistryMonitorController implements Serializable{
    private static final Logger logger = LoggerFactory.getLogger(RegistryMonitorController.class);
    private String newKey;
    private String newValue;

    @EJB
    RegistryService registry;

    public RegistryService getRegistry() {
        return registry;
    }

    public String getNewKey() {
        return newKey;
    }

    public void setNewKey(String newKey) {
        this.newKey = newKey;
    }

    public String getNewValue() {
        return newValue;
    }

    public void setNewValue(String newValue) {
        this.newValue = newValue;
    }

    public void addProperty(){
        if(newKey!= null && newValue!= null)
            registry.setProperty(newKey,newValue);
        newKey=null;
        newValue=null;
    }

    public void removeProperty(){
        String key = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("key");
        registry.removeProperty(key);
    }

    public void mouseOver(AjaxBehaviorEvent e) {
        HtmlInputText input = (HtmlInputText) e.getComponent();
        input.setStyle("background-color:#ff9");
    }

    public void mouseOut(AjaxBehaviorEvent e) {
        HtmlInputText input = (HtmlInputText) e.getComponent();
        input.setStyle("background-color:#fff");
    }
}
